package mi.payment.utils.transaction;

import android.content.Context;

import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 批上送结束
 *
 * @author leeshenzhou on 2017/2/8.
 */
class BatchUploadFinancialTask extends TransactionTask<Object, Void, String> {

    BatchUploadFinancialTask(Context context) {
        super(context, false);
    }

    @Override
    protected void onBegin(Object[] objects) {
        tradeData = (TradeData) objects[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected String appointResult() {
        return resultStr;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_BATCH_UPLOAD_FINANCIAL);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        String value48 = (String) params[1];
        String netCode = (String) params[2];
        String bathNo = dbBean.getBathNo();

        // 更新预组装的Map
        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写48域
        MsgField fieldF048 = map.get("F048");
        fieldF048.setDataStr(value48);
        fieldF048.setDataLength(value48.length());

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        String value60 = "00" + bathNo + netCode;
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 组包，得到组包后的报文
        return EncodePackage.assembly(map, "0320", TerminalSecure.PROCESSREQUEST_NONE);
    }


}
